﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using mendian;
using Luna小店.DTO;
using DevExpress.XtraGrid.Demos.util;
using Luna小店.UC.面料库存.面料出库.service;

namespace Luna小店.UC.面料库存
{
    public partial class Form面料出库单异常 : DevExpress.XtraEditors.XtraForm
    {
        public List<DTO异常> DTO异常s = new List<DTO异常>();
        public Dictionary<string,List<DTO异常>> DTO异常map = new Dictionary<string, List<DTO异常>>();
        private string orderId;
        public Form面料出库单异常(string orderId,List<DTO异常> exception)
        {
            InitializeComponent();
            this.orderId = orderId;
            this.DTO异常s = exception;
            this.gridControl异常.DataSource = this.DTO异常s;
        }

        private void simpleButton添加_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(this.textEdit调整值.Text))
            {
                MessageBox.Show("请填写调整值");
                return;
            }
            this.DTO异常s.Add(
                new DTO异常(
                    this.comboBox调整原因.Text,
                    Convert.ToDouble(this.textEdit调整值.Text),
                    this.dateTimePicker时间.Value
                    )
                );
            this.gridView异常.RefreshData();
        }

        private void toolStripMenuItem删除_Click(object sender, EventArgs e)
        {
            MessageBoxButtons messBtn = MessageBoxButtons.OKCancel;
            DialogResult dr = MessageBox.Show("确定删除选中行？", "提示:确认", messBtn);
            if (dr == DialogResult.OK)
            {
                string id = FunctionHelper.GetgridViewValue(
                    this.gridView异常,
                    this.gridView异常.FocusedRowHandle,
                    "id"
                    );
                this.DTO异常s.Remove(
                    this.DTO异常s[this.gridView异常.FocusedRowHandle]
                        );
                //根据id删除异常调整项
                StockOutService.post删除异常调整(id);
                MessageBox.Show("删除成功");
                this.gridView异常.RefreshData();
            }
            else
            {
                return;
            }
            
        }
        private void simpleButton确认_Click(object sender, EventArgs e)
        {
            this.DTO异常map.Add(this.orderId, this.DTO异常s);
            //调用service添加异常调整
            StockOutService.post确认添加异常调整(this.DTO异常map);
            this.Close();
        }

        private void textEdit调整值_KeyPress(object sender, KeyPressEventArgs e)
        {
            //允许输入数字、小数点、删除键和负号  
            if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != (char)('.')&& e.KeyChar != (char)('-'))
            {
                //MessageBox.Show("请输入正确的数字");
                this.textEdit调整值.Text = "";
                e.Handled = true;
            }
            if (e.KeyChar == (char)('-'))
            {
                if (textEdit调整值.Text != "")
                {
                    //MessageBox.Show("请输入正确的数字");
                    this.textEdit调整值.Text = "";
                    e.Handled = true;
                }
            }
            //小数点只能输入一次  
            if (e.KeyChar == (char)('.') && ((TextEdit)sender).Text.IndexOf('.') != -1)
            {
                //MessageBox.Show("请输入正确的数字");
                this.textEdit调整值.Text = "";
                e.Handled = true;
            }
            //第一位不能为小数点  
            if (e.KeyChar == (char)('.') && ((TextEdit)sender).Text == "")
            {
                //MessageBox.Show("请输入正确的数字");
                this.textEdit调整值.Text = "";
                e.Handled = true;
            }
            //第一位是0，第二位必须为小数点  
            if (e.KeyChar != (char)('.') && ((TextEdit)sender).Text == "0")
            {
                //MessageBox.Show("请输入正确的数字");
                this.textEdit调整值.Text = "";
                e.Handled = true;
            }
            //第一位是负号，第二位不能为小数点  
            if (((TextEdit)sender).Text == "-" && e.KeyChar == (char)('.'))
            {
                //MessageBox.Show("请输入正确的数字");
                this.textEdit调整值.Text = "";
                e.Handled = true;
            }
        }
        
    }
}